def file_data(file_load, head=False) ->dict:
    data = {}
    file_text = open(file_load, 'r', encoding='utf-8').read()
    file_text = file_text.split('\n')
    if head == True:
        file_text.pop(0)
    for i in range(len(file_text)):
        file_text[i] = file_text[i].split(',')
        data[file_text[i].pop(0)] = file_text[i]
    return data


def get_var_name(variable) -> str:
    globals_dict = globals()
    name_list = []
    for var_name in globals_dict:
        if globals_dict[var_name] is variable:
            name_list.append(var_name)
    for i in range(len(name_list)):
        if name_list[i] != 'my_var':
            return name_list[i]


#字符串转换
def class_str(every_object) -> str:
    if type(every_object) != type(str()):
        every_object = str(every_object)
        return every_object
    elif type(every_object) == type(str()):
        return every_object


class Data_Process(object):
    def __init__(self, List=None, Dict=None, file_load=None, Data_Process=None):
        #print("Data_Process以构造")
        self.__dataform = {}
        if List != None and file_load == None and Dict == None and Data_Process == None:
            self.__dataform = {0: List}
        elif List == None and file_load != None and Dict == None and Data_Process == None:
            self.__file_load = file_load
            self.__dataform = file_data(self.__file_load)
        elif Dict != None and List == None and file_load == None and Data_Process == None:
            add_key = list(Dict.keys())
            for i in range(len(add_key)):
                if type(Dict[add_key[i]]) != type(list()) and type(Dict[add_key[i]]) != type(int()):
                    self.__dataform[add_key[i]] = list(Dict[add_key[i]])
                elif type(Dict[add_key[i]]) != type(list()) and type(Dict[add_key[i]]) == type(int()):
                    self.__dataform[add_key[i]] = [Dict[add_key[i]]]
                elif type(Dict[add_key[i]]) == type(list()):
                    self.__dataform[add_key[i]] = Dict[add_key[i]]
        elif List == None and Dict == None and file_load == None and Data_Process != None:
            self.__dataform = Data_Process.get_data()
        elif List == None and Dict == None and file_load == None and Data_Process == None:
            self.__dataform = {0: []}

    def __del__(self):
        return
        #print("{}析构函数已调用".format(get_var_name(self)))

    # 索引重载
    def __getitem__(self, column=None):
        dict_key = list(self.get_data().keys())
        if type(column) == type(int()):
            return self.get_data()[dict_key[column]]
        elif type(column) == type(tuple()) and 2 >= len(column) >= 1:
            if len(column) == 1 and column[0] != None:
                return self.get_data()[dict_key[column[0]]]
            elif len(column) == 2 and column[0] != None and column[1] == None:
                return self.get_data()[dict_key[column[0]]]
            elif len(column) == 2 and column[0] != None and column[1] != None:
                return self.get_data()[dict_key[column[0]]][column[1]]
            elif len(column) == 2 and column[0] == None and column[1] != None:
                result_list = []
                for i in range(len(dict_key)):
                    result_list.append(self.get_data()[dict_key[i]][column[1]])
                return result_list

    # 索引赋值重载
    def __setitem__(self, column=None, value=None):
        dict_key = list(self.get_data().keys())
        if type(column) == type(tuple()) and len(column) == 2:
            if len(column) == 2 and column[0] != None and column[1] != None:
                self.__dataform[dict_key[column[0]]][column[1]] = value
            elif len(column) == 2 and column[0] != None and column[1] == None:
                self.__dataform[dict_key[column[0]]] = value
            elif len(column) == 2 and column[0] == None and column[1] != None:
                result_list = []
                for i in range(len(value)):
                    self.__dataform[dict_key[i]][column[1]] = value[i]

    # 打印
    def __str__(self) -> str:
        add_key = list(self.__dataform.keys())
        result_str = ''
        for i in range(len(add_key)):
            result_str += "{}   {}\n".format(add_key[i], self.__dataform[add_key[i]])
        return result_str

    # 深拷贝
    def __deep_copy(self) -> dict:
        add_key = list(self.__dataform.keys())
        new_dict = {}
        for i in range(len(add_key)):
            new_dict[add_key[i]] = self.__dataform[add_key[i]]
        return new_dict


    # 取值
    def get_data(self) -> dict:
        return self.__deep_copy()

    # 修改dataform
    def set_data(self, Dict=None, dataform=None):
        if Dict != None and dataform == None:
            self.__dataform = Dict
        elif Dict == None and dataform != None:
            self.__dataform = dataform.get_data()

    # 修改行索引
    def set_row(self, row):
        dict_key = list(self.__dataform.keys())
        new_dataform = {}
        if len(list(self.__dataform.keys())) <= len(row):
            for i in range(len(row)):
                if i >= len(list(self.get_data().keys())):
                    new_dataform[row[i]] = []
                elif i < len(list(self.get_data().keys())):
                    new_dataform[row[i]] = self.__dataform[dict_key[i]]
        elif len(list(self.__dataform.keys())) > len(row):
            for i in range(len(list(self.__dataform.keys()))):
                if i > len(row):
                    self.row_pop()
                elif i <= len(row):
                    new_dataform[row[i]] = self.__dataform[dict_key[i]]
        self.set_data(Dict=new_dataform)

    # 删除指定行
    def row_pop(self, index=None):
        dict_key = list(self.__dataform.keys())
        if index == None:
            self.__dataform.pop(dict_key[-1])
        elif index != None:
            self.__dataform.pop(index)

    # 删除指定列
    def col_pop(self, index=None):
        dict_key = list(self.__dataform.keys())
        for i in range(len(dict_key)):
            if index == None:
                self.__dataform[dict_key[i]].pop()
            elif index != None:
                self.__dataform[dict_key[i]].pop(index)
                
                
    #添加行
    def add_line(self, List, index = None):
        if index == None:
            self.__dataform[len(self.__dataform) + 1] = List
        else:
            self.__dataform[index] = List

    # 拼接
    def merge(self, add_dataform):
        add_key = list(add_dataform.get_data().keys())
        for i in range(len(add_key)):
            self.__dataform[add_key[0]] = add_dataform.get_data()[add_key[0]]

    # 空值检测
    def null_value(self):
        null_count = 0
        dataform_row = list(self.__dataform.keys())
        for i in range(len(dataform_row)):
            if dataform_row[i] == 'null' or dataform_row[i] == 'Null':
                null_count += 1
            elif dataform_row[i] == 'none' or dataform_row[i] == 'None':
                null_count += 1
            elif dataform_row[i] == '':
                null_count += 1
            elif dataform_row[i] == 'nan' or dataform_row[i] == 'NaN':
                null_count += 1
        return null_count
        
        
        
        #样本个数
    def count(self):
        return len(list(self.get_data().keys()))
        
        
    #字符串转换成整数
    def to_int(self):
        dict_key = list(self.__dataform.keys())
        for i in range(len(dict_key)):
            new_value = []
            for j in range(len(self.__dataform[dict_key[i]])):
                num = int(self.get_data()[dict_key[i]][j])
                new_value.append(num)
            self.__dataform[dict_key[i]] = new_value
        print("convert network")


    #字符串转换成浮点数
    def to_double(self):
        dict_key = list(self.__dataform.keys())
        for i in range(len(dict_key)):
            new_value = []
            for j in range(len(self.__dataform[dict_key[i]])):
                num = float(self.get_data()[dict_key[i]][j])
                new_value.append(num)
            self.__dataform[dict_key[i]] = new_value
        print("convert network")
        
        

    # 保存成csv
    def writer_csv(self, file_load):
        f = open(file_load, 'w')
        file_str = ''
        dataform_index = list(self.__dataform.keys())
        for i in range(len(dataform_index)):
            file_str += class_str(dataform_index[i]) + ','
            for j in range(len(self.__dataform[dataform_index[i]])):
                file_str += class_str(self.__dataform[dataform_index[i]][j]) + ','
            file_str += '\n'
        f.write(file_str)
        f.close()




